<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validateTrigger="bind">
      <uni-forms-item name="appname" label="软件名称">
        <uni-easyinput placeholder="网站、APP、小程序通用名称" v-model="formData.appname" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="WebUrl" label="H5手机网站地址">
        <uni-easyinput placeholder="H5手机网站地址" v-model="formData.WebUrl" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="APIjiekoUrl" label="API接口地址">
        <uni-easyinput placeholder="填写苹果CMS安装路径，在根目录填域名，在子目录填域名+目录" v-model="formData.APIjiekoUrl" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="About" label="关于我们">
        <uni-easyinput placeholder="关于我们-描述信息" v-model="formData.About" trim="right"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="ICPbeanhao" label="ICP备案号">
        <uni-easyinput placeholder="ICP备案号" v-model="formData.ICPbeanhao" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="adminQQ" label="QQ号">
        <uni-easyinput placeholder="QQ号" type="number" v-model="formData.adminQQ"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="adminWX" label="微信号">
        <uni-easyinput placeholder="微信号" v-model="formData.adminWX" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="adminAlipay" label="支付宝">
        <uni-easyinput placeholder="支付宝账号" type="number" v-model="formData.adminAlipay"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="adminEmail" label="Email邮箱">
        <uni-easyinput placeholder="Email邮箱" v-model="formData.adminEmail" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="appUser_jieshao" label="App介绍">
        <uni-easyinput placeholder="App介绍" v-model="formData.appUser_jieshao" trim="right"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="app_iconImg_file" label="app图标" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.app_iconImg_file"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="app_Web_appdownload" label="app下载地址">
        <uni-easyinput placeholder="app下载地址，可复制[升级中心]上传的APP>>包地址" v-model="formData.app_Web_appdownload" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="share_appSDK_type" label="分享调用SDK类型">
        <uni-data-checkbox v-model="formData.share_appSDK_type" :localdata="formOptions.share_appSDK_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="share_title" label="分享内容的标题">
        <uni-easyinput placeholder="分享内容的标题" v-model="formData.share_title" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="share_imageUrl_file" label="分享内容的图片" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.share_imageUrl_file"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="share_summary" label="分享内容的摘要">
        <uni-easyinput placeholder="分享类型为[纯文字]时必填" v-model="formData.share_summary" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="share_type" label="分享类型">
        <uni-data-checkbox v-model="formData.share_type" :localdata="formOptions.share_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="share_href" label="分享跳转链接">
        <uni-easyinput placeholder="如APP下载地址，APP官网等" v-model="formData.share_href" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="share_erweimUrl_file" label="分享二维码" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.share_erweimUrl_file"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="App_Player_zyjx_jieko" label="解析接口">
        <textarea placeholder="解析接口,一行一个" @input="binddata('App_Player_zyjx_jieko', $event.detail.value)" class="uni-textarea-border" v-model="formData.App_Player_zyjx_jieko"></textarea>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
            <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/opendb-admin-xtsetting.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'opendb-admin-xtsetting';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
    data() {
      let formData = {
        "appname": "",
        "WebUrl": "",
        "APIjiekoUrl": "",
        "About": "",
        "ICPbeanhao": "",
        "adminQQ": null,
        "adminWX": "",
        "adminAlipay": null,
        "adminEmail": "",
        "appUser_jieshao": "",
        "app_iconImg_file": null,
        "app_Web_appdownload": "",
        "share_appSDK_type": 2,
        "share_title": "",
        "share_imageUrl_file": null,
        "share_summary": "",
        "share_type": 1,
        "share_href": "",
        "share_erweimUrl_file": null,
        "App_Player_zyjx_jieko": ""
      }
      return {
        formData,
        formOptions: {
          "share_appSDK_type_localdata": [
            {
              "text": "调用社交sdk分享,需在manifest.json的sdk配置中配的分享sdk厂商，与本机安装了什么社交App无关",
              "value": 1
            },
            {
              "text": "调用系统分享组件发送分享消息，不需要配置分享SDK",
              "value": 2
            }
          ],
          "share_type_localdata": [
            {
              "text": "图文",
              "value": 1
            },
            {
              "text": "纯文字",
              "value": 2
            },
            {
              "text": "纯图片",
              "value": 3
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 触发表单提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          this.submitForm(res)
        }).catch(() => {
          uni.hideLoading()
        })
      },

      submitForm(value) {
        // 使用 clientDB 提交数据
        db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("appname,WebUrl,APIjiekoUrl,About,ICPbeanhao,adminQQ,adminWX,adminAlipay,adminEmail,appUser_jieshao,app_iconImg_file,app_Web_appdownload,share_appSDK_type,share_title,share_imageUrl_file,share_summary,share_type,share_href,share_erweimUrl_file,App_Player_zyjx_jieko").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
